package cn.zhentao.controller;

import cn.zhentao.borrow.dto.NoticeParam;
import cn.zhentao.borrow.pojo.TNotice;
import cn.zhentao.common.Result;
import cn.zhentao.service.TNoticeService;
import cn.zhentao.service.TUserService;
import cn.zhentao.user.pojo.TUser;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("notice")
public class TNoticeController {
    @Autowired
    private TNoticeService noticeService;
    @Autowired
    private TUserService tUserService;

    @PostMapping("noticeAdd")
    public Result noticeAdd(@RequestBody NoticeParam param){
        if(param.getUid().length==0 || StringUtils.isBlank(param.getContext()) || StringUtils.isBlank(param.getTitle())){
            return Result.ERROR("参数不能为空");
        }
        for (Integer uid : param.getUid()) {
            TNotice tNotice = new TNotice();
            tNotice.setUserId(uid);
            tNotice.setContent(param.getContext());
            tNotice.setContentTitle(param.getTitle());
            noticeService.save(tNotice);
        }
       return Result.OK("通知成功");
    }
   //通知查询
    @GetMapping("selByuid")
    public Result selByuid(@RequestParam("id") Integer id){
        List<TNotice> list = noticeService.selbyuid(id);
        return Result.OK(list);
    }
    //消息改变成已读状态
    @GetMapping("msage_yidu")
    public void msage_yidu(@RequestParam("id") Integer id){
        TNotice byId = noticeService.getById(id);
        byId.setStatus(1); //已读
        noticeService.updateById(byId);
    }

    //新书上架通知
    @PostMapping("book_upnacote")
    public Result book_upnacote(@RequestBody NoticeParam param){
        if(StringUtils.isBlank(param.getContext()) || StringUtils.isBlank(param.getTitle())){
            return Result.ERROR("参数不能为空");
        }
        List<TUser> list = tUserService.list();
        for (TUser user : list) {
            TNotice tNotice = new TNotice();
            tNotice.setUserId(user.getId());
            tNotice.setContent(param.getContext());
            tNotice.setContentTitle(param.getTitle());
            noticeService.save(tNotice);
        }
        return Result.OK("已通知所有用户哦");
    }

    @DeleteMapping("nacote_del/{id}")
    public Result nacote_del(@PathVariable("id") Integer id){
        return noticeService.removeById(id)?Result.OK("删除成功"):Result.ERROR("删除失败");
    }

    @GetMapping("Reser_message")
    public Result Reser_message(@RequestParam("uid") Integer uid){
        if(uid == null){
            return Result.ERROR("参数不能为空");
        }
        int i=0;
        List<TNotice> one = noticeService.list(Wrappers.<TNotice>lambdaQuery().eq(TNotice::getUserId, uid));
        if(one.size()>0){  //大于0说明存在存在就给提示
            for (TNotice tNotice : one) {
                if(tNotice.getStatus() == 0){  //未读消息
                    ++i;
                }
            }
            return Result.OK("您有"+i+"条未读的消息哦");
        }

        return Result.ERROR("您没有消息");
    }
}
